<?php
class omeauto_mdl_autoconfirm extends dbeav_model{
    function suf_delete($oid){
        $orderTypeObj = app::get('omeauto')->model('order_type');
        $orderTypeObj->update(array('oid'=>0),array('oid'=>$oid));
        return true;
    }

	var $has_export_cnf = true;
    public $export_name = '自动审单规则';

    function getselectdata($filter, $offset, $limit)
    {
        $list = $this->getList('*', $filter, $offset, $limit);
        $oMdl = app::get('omeauto')->model('order_type');
        $listData=array();
        $i=0;
        foreach ($list as $k => $v) 
        {
            $autodata = $oMdl->getList("*",array('oid'=>$v['oid']));
            foreach ($autodata as $key => $value) 
            {
                $listData[$i]['name'] = ($v['name'] ? $v['name'] : '-');
                $listData[$i]['memo'] = ($v['memo'] ? $v['memo'] : '-');
                
                $html = '';
		        if (!empty($v['config'])) 
		        {
		            if ($v['config']['autoConfirm'] == 1) 
		            {
		                $html .= ($v['config']['morder'] == 1 ) ? "同一买家有多个订单并且收货地址不一致<br/>" : "";
		                $html .= ($v['config']['payStatus'] == 1 ) ? "同一买家还有未支付订单<br/>" : "";
		                $html .= ($v['config']['memo'] == 1 || $v['config']['memo'] == 'on') ? "有客户留言<br/>" : "";
		                $html .= ($v['config']['mark'] == 1 || $v['config']['mark'] == 'on') ? "有客服备注<br/>" : "";
		                $html .= ($v['config']['autoCod'] == 1 ) ? "符合其它条件，可自动确认货到付款订单<br/>" : "需要人工审核货到付款订单<br/>";
		                $html .= ($v['config']['allDlyCrop'] == 1 ) ? "禁用全境物流<br/>" : "启用全境物流<br/>";
		            } else {
		                $html .= "自动审核关闭，全部订单需人工处理";
		            }
		        }
                $listData[$i]['content'] = $html;
                $listData[$i]['oname'] = ($value['name'] ? $value['name'] : '-');
                $listData[$i]['odesc'] = ($value['memo'] ? $value['memo'] : '-');
                $html = '';
		        if (!empty($value['config'])) {
		            
		            foreach ($value['config'] as $row) {
		                
		                $role = json_decode($row, true);
		                $html .= $role['caption'] . "<br/>";
		            }
		        }
                $listData[$i]['ocontent'] =$html;
                $i=$i+1;
            }
        }
        return $listData;
    }
    function getExportDataByCustom($fields, $filter, $has_detail, $curr_sheet, $start, $end)
    {
        if($curr_sheet == 1){
            $title = array();
            foreach ($this->io_title('confirm') as $k => $v) {
                $title[] = mb_convert_encoding($v, 'GBK', 'UTF-8');
            }
            $data['content']['main'][0] = '"' . implode('","', $title) . '"';
        }
        $list = $this->getselectdata($filter, $start, $end);
        foreach ($list as $k => $aFilter) {
            $exptmp_data = array();
            foreach ($aFilter as $col => $val) {
                $aFilter[$col] = mb_convert_encoding($aFilter[$col], 'GBK', 'UTF-8');
                $exptmp_data[] = $aFilter[$col];
            }
            $data['content']['main'][] = implode(',', $exptmp_data);
        }
        return $data;
    }
    function io_title($filter, $ioType = 'csv')
    {
        switch ($filter) {
            case 'confirm':
                $this->oSchema['csv'][$filter] = array(
                    '*:审单名称' => 'name',
                    '*:审单描述' => 'memo',
                    '*:检查内容' => 'content',
                    '*:订单小组' => 'oname',
                    '*:简单说明' => 'odesc',
                    '*:类型内容' => 'ocontent',
                );
                break;
        }
        $this->ioTitle[$ioType][$filter] = array_keys($this->oSchema[$ioType][$filter]);
        return $this->ioTitle[$ioType][$filter];
    }


    public function get_schema_export()
    {
        $schema = array(
            'columns' => array(
                'name' => array (
                    'type' => 'varchar(50)',
                    'required' => false,
                    'editable' => false,
                    'label' => '审单名称',
                    'in_list' => false,
                    'default_in_list' => false,
                    'width' => 150,
                    'order' => 40
                ),
                'memo' => array (
                    'type' => 'varchar(50)',
                    'required' => false,
                    'editable' => false,
                    'label' => '审单描述',
                    'in_list' => false,
                    'default_in_list' => false,
                    'width' => 150,
                    'order' => 40
                ),
                'content' => array (
                    'type' => 'varchar(500)',
                    'required' => false,
                    'editable' => false,
                    'label' => '检查内容',
                    'in_list' => false,
                    'default_in_list' => false,
                    'width' => 150,
                    'order' => 40
                ),
                'oname' => array (
                    'type' => 'varchar(50)',
                    'required' => false,
                    'editable' => false,
                    'label' => '订单小组',
                    'in_list' => false,
                    'default_in_list' => false,
                    'width' => 150,
                    'order' => 40
                ),
                'odesc' => array (
                    'type' => 'varchar(500)',
                    'required' => false,
                    'editable' => false,
                    'label' => '简单说明',
                    'in_list' => false,
                    'default_in_list' => false,
                    'width' => 150,
                    'order' => 40
                ),
                'ocontent' => array (
                    'type' => 'varchar(500)',
                    'required' => false,
                    'editable' => false,
                    'label' => '类型内容',
                    'in_list' => false,
                    'default_in_list' => false,
                    'width' => 150,
                    'order' => 40
                ),
            ),
            'idColumn' => 'name',
            'in_list' => array(
                0 => 'name',
                1 => 'memo',
                2 => 'content',
                3 => 'oname',
                4 => 'odesc',
                5 => 'ocontent',
            ),
            'default_in_list' => array(
                0 => 'name',
                1 => 'memo',
                2 => 'content',
                3 => 'oname',
                4 => 'odesc',
                5 => 'ocontent',
            ),
        );
        return $schema;
    }



    public function getExportTitle($fields)
    {
        $title = array();
        $main_columns = $this->get_schema_export();
        foreach (explode(',', $fields) as $k => $col) {
            if (isset($main_columns['columns'][$col])) {
                $title[] = "*:" . $main_columns['columns'][$col]['label'];
            }
        }

        return mb_convert_encoding(implode(',', $title), 'GBK', 'UTF-8');
    }
}